package com.song.boot.springstudy.thread.synchronizedtest;

import lombok.SneakyThrows;

/**
 * 测试sync
 * CounterThis     321
 *CounterCode   322
 * CounterStatic   308
 */
public class CounterTest {
    @SneakyThrows
    public static void main(String[] args) {
        // 1. 记录开始时间
        long startTime = System.currentTimeMillis();
        // CounterThis counter = new CounterThis();
        // CounterCode counter = new CounterCode();
        CounterStatic counter = new CounterStatic();
        // count
        for (int i = 0, size = 1000; i < size; i++) {
            Thread thread = new Thread(() -> {
                counter.addCount();
            });
            thread.start();
            thread.join();
        }
        // total
        for (int i = 0, size = 1000; i < size; i++) {
            Thread thread = new Thread(() -> {
                counter.addTotal();
            });
            thread.start();
            thread.join();
        }

        System.out.println("count:::"+counter.count);
        System.out.println("total:::"+counter.total);
        // 3. 记录结束时间
        long endTime = System.currentTimeMillis();

        // 4. 计算并打印耗时
        long costTime = endTime - startTime;
        System.out.println("方法执行耗时：" + costTime + " 毫秒");
    }
}